<%@ page import="org.apache.commons.fileupload.*, org.apache.commons.fileupload.servlet.ServletFileUpload, org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.io.FilenameUtils, java.util.*, java.io.File, java.lang.Exception" %>
<%
Integer step = new Integer(1);
String recordForMeStr = request.getParameter("record_for_me");
String readingId = request.getParameter("id");
boolean recordForMe;
if (recordForMeStr != null){
	recordForMe = true;
}
else{
	recordForMe = false;
}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>File Upload Example</title>
    <style type="text/javascript">
body{
	text-align: center;
}
</style>
  </head>
  <body>
  <%@ include file="/user/load_user_with_redirect.jsp" %>
<%@ include file="steps.jsp" %>
      <div style="width: 600px; margin:auto; text-align:left;">
    <p>
<%

if (ServletFileUpload.isMultipartContent(request)){
  ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
  List fileItemsList = servletFileUpload.parseRequest(request);

  String optionalFileName = "";
  FileItem fileItem = null;

  Iterator it = fileItemsList.iterator();
  boolean hasFile = false;
  while (it.hasNext()){	
    FileItem fileItemTemp = (FileItem)it.next();
    if (fileItemTemp.isFormField()){
		
%>

<b>Name-value Pair Info:</b><br/>
Field name: <%= fileItemTemp.getFieldName() %><br/>
Field value: <%= fileItemTemp.getString() %><br/><br/>
<%
      if (fileItemTemp.getFieldName().equals("filename"))
        optionalFileName = fileItemTemp.getString();
    }
    else
      fileItem = fileItemTemp;
  }

  if (fileItem!=null){
    String fileName = fileItem.getName();
	if (fileName != null && !fileName.equals("")){
		hasFile = true;
%>

<b>Uploaded File Info:</b><br/>
Content type: <%= fileItem.getContentType() %><br/>
Field name: <%= fileItem.getFieldName() %><br/>
File name: <%= fileName %><br/>
File size: <%= fileItem.getSize() %><br/><br/>

<%
	}
    /* Save the uploaded file if its size is greater than 0. */
    if (fileItem.getSize() > 0){
      if (optionalFileName.trim().equals(""))
        fileName = FilenameUtils.getName(fileName);
      else
        fileName = optionalFileName;

      String dirName = "/LG/uploads/readings/";

      File saveTo = new File(dirName + readingId + ".wav"); // fileName
	  if (saveTo.exists()){
		System.out.println("copy to new loc");
		 File newFile = new File(dirName + readingId + "-1"); // fileName
	  }
	  
      try {
        fileItem.write(saveTo);
		System.out.println("readingId:" + readingId);
		
		String name = request.getParameter("fileName");
		String path = request.getParameter("path");
		org.languageguide.readings.FileManager.addFile(readingId, fileName, path, user);
%>

<b>The uploaded file has been saved successfully.</b>
<form id="form1" name="form1" method="post" action="/user/readings/reading_continue_to_integration.jsp?id=<%= readingId  %>">
    <input name="Submit" type="submit" value="Continue on to Step 3" style="font-size: 120%; margin:20px; padding: 10px;"/>
    </form>
	

<%
      }
      catch (Exception e){
e.printStackTrace();
	out.println("error" + e.getMessage());

%>

<b>An error occurred when we tried to save the uploaded file. </b>

<%
      }
    }

  }
 
}
%>
    </div>
  </body>
</html>